package com.sscw.task.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.guaike.core.mybatis.plugins.BaseDAO;
import com.sscw.task.entity.MRecordBattleArcadeMachinePO;
import com.sscw.task.mapper.MRecordBattleArcadeMachineMapper;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;

@Repository
public class MRecordBattleArcadeMachineDAO extends BaseDAO<MRecordBattleArcadeMachineMapper, MRecordBattleArcadeMachinePO> {

    public MRecordBattleArcadeMachinePO findOneByUserId(Long battleId, Long userId) {
        LambdaQueryWrapper<MRecordBattleArcadeMachinePO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MRecordBattleArcadeMachinePO::getBattleId, battleId);
        queryWrapper.eq(MRecordBattleArcadeMachinePO::getUserId, userId);
        return getBaseMapper().selectOne(queryWrapper);
    }

    public MRecordBattleArcadeMachinePO findLatestBattleByUserId(Long userId) {
        LambdaQueryWrapper<MRecordBattleArcadeMachinePO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MRecordBattleArcadeMachinePO::getBattleDate, LocalDate.now());
        queryWrapper.eq(MRecordBattleArcadeMachinePO::getUserId, userId);
        queryWrapper.orderByDesc(MRecordBattleArcadeMachinePO::getId);
        queryWrapper.last("limit 0,1");
        return getBaseMapper().selectOne(queryWrapper);
    }

    public long countUserTodayBattle(Long userId) {
        LambdaQueryWrapper<MRecordBattleArcadeMachinePO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MRecordBattleArcadeMachinePO::getBattleDate, LocalDate.now());
        queryWrapper.eq(MRecordBattleArcadeMachinePO::getUserId, userId);
        return getBaseMapper().selectCount(queryWrapper);
    }
}
